/*
* $Id: InspectForm.java 255 2011-01-26 17:42:13Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextField;

import apiary.Apiary;
import apiary.settings.SettingStore;
import apiary.settings.SettingsRecord;
import apiary.ui.BackScreen;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;

class InspectForm extends BackScreen implements CommandListener
{
    InspectValue inspect;

    public InspectForm(InspectValue ins) {
        super(ins.nameHousing);
        inspect = ins;
        Apiary apiary = Apiary.getInstance();
        append(new TextField(apiary.getLocaleString("Honey"),
                String.valueOf(inspect.honey), 256, TextField.NUMERIC));
        append(new TextField(apiary.getLocaleString("Brood"),
                String.valueOf(inspect.brood), 256, TextField.NUMERIC));
        SettingStore settings = SettingStore.getInstance();
        if(settings.getData(SettingsRecord.OPEN_BROOD).getValue() == Boolean.TRUE)
            append(new TextField(apiary.getLocaleString("OpenBrood"),
                    String.valueOf(inspect.openBrood), 256, TextField.NUMERIC));
        if(settings.getData(SettingsRecord.SEAL_BROOD).getValue() == Boolean.TRUE)
            append(new TextField(apiary.getLocaleString("SealBrood"),
                    String.valueOf(inspect.closeBrood), 256, TextField.NUMERIC));
        if(settings.getData(SettingsRecord.GOODS_HONEY).getValue() == Boolean.TRUE)
            append(new TextField(apiary.getLocaleString("GoodsHoney"),
                    String.valueOf(inspect.goodsHoney), 256, TextField.NUMERIC));

        addCommand(new Command(apiary.getLocaleString("Save"), Command.OK, 0));
        addCommand(new Command(apiary.getLocaleString("Back"), Command.BACK, 0));
        setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if(c.getCommandType() == Command.OK) {
            int offset = 2;
            TextField field = (TextField) get(0);
            inspect.honey = Integer.parseInt(field.getString());
            field = (TextField) get(1);
            inspect.brood = Integer.parseInt(field.getString());
            SettingStore settings = SettingStore.getInstance();
            if(settings.getData(SettingsRecord.OPEN_BROOD).getValue() == Boolean.TRUE) {
                field = (TextField) get(offset);
                inspect.openBrood = Integer.parseInt(field.getString());
                offset++;
            }
            if(settings.getData(SettingsRecord.SEAL_BROOD).getValue() == Boolean.TRUE) {
                field = (TextField) get(offset);
                inspect.closeBrood = Integer.parseInt(field.getString());
                offset++;
            }
            if(settings.getData(SettingsRecord.GOODS_HONEY).getValue() == Boolean.TRUE) {
                field = (TextField) get(offset);
                inspect.goodsHoney = Integer.parseInt(field.getString());
                offset++;
            }
        }
        Apiary.getInstance().Display(backForm);
    }
}
